package project.main;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import project.crawler.downloader.PageDownloader;
import project.dbaccesslayer.MySQLAccessLayer;
import project.dbaccesslayer.Table_SO_Users_Raw_html;

public class DownloadUsers {

	public static void main(String[] args) throws IOException, SQLException {
		if (args.length != 1) {
			System.out.println("Usage: DownloadUsers <limit>");
			return;
		}
		int limit = Integer.parseInt(args[0]);

		Connection conn = MySQLAccessLayer.openConnectionToMySql();

		while (limit > 0) {
			List<Long> list = Table_SO_Users_Raw_html.getUsersToDownload(conn);
			System.out.println("Got " + list.size() + " users to download.");

			if (list.isEmpty()) {
				break;
			}

			for (long userID : list) {
				String html = PageDownloader.downloadUserRawHtml(userID);
				Table_SO_Users_Raw_html.saveUserRawHtml(conn, userID, html);
			}

			limit -= list.size();
		}

		System.out.println("Done.");

		conn.close();
	}

}
